<template>
  <div id="login" @keyup.13="doLogin">
    <header class="login_head">
      <div class="head-content">
        <div class="logo">
          <img src="../../../static/images/login_logo.png" class="img-responsive" />
        </div>
      </div>
    </header>
    <section class="login_main">
      <div class="container clearfix">
        <div class="ctn_box pull-right">
          <div class="form-menu clearfix">
            <h2 class="input-title text-center">请登录</h2>
            <div class="loginLine"></div>
            <div class="clearfix login-inputBox">
              <label for="loginName" class="pull-left loginLable1">
                <i></i>
              </label>
              <div class="pull-right">
                <input type="text" id="txtLoginName" v-model="userName" width="100%" />
              </div>
            </div>
            <div class="clearfix login-inputBox">
              <label for="loginPass" class="pull-left loginLable2">
                <i></i>
              </label>
              <div class="pull-right">
                <input
                  type="password"
                  id="txtPassword"
                  v-model="password"
                  width="100%"
                  maxlength="16"
                />
              </div>
            </div>
            <div class="wrapper">
              <span>
                <input type="checkbox" class="input_check" v-model="checked" id="check1" />
                <label for="check1"></label>
                <label>记住密码</label>
              </span>
            </div>
            <div>
              <button type="submit" id="ImageButton1" @click="doLogin()" class="submit-btn">登 录</button>
            </div>
          </div>
        </div>
      </div>
    </section>
    <footer>
      <p class="version">Copyright&copy;2018 , Ver 1.1.4.05041， 北京大学深圳医院版权所有</p>
    </footer>
  </div>
</template>
<script>
import { apiLogin } from "@/api/api"; // 导入我们的api接口

export default {
  name: "Login",
  data() {
    return {
      userName: JSON.parse(localStorage.getItem("account")), //用户名
      password: JSON.parse(localStorage.getItem("password")), //密码
      // userName: '',
      // password: '',
      checked: true,
      position: "",
      contents: "",

      patientId: "",
      flag: false
    };
  },
  created() {
    // 自动登录
    this.autoLogin();
    // console.log(window.location.search.split('patientId=')[1])
    // this.$nextTick(function(){
    // let patientId = 'patientId'
    // this.getQueryVariable(patientId)
    // })
    // console.log(this.patientId)
  },
  mounted() {},
  methods: {
    // 自动登录
    // autoLogin(){
    //   let query = window.location.search.substring(1);
    //   let vars = query.split("&");
    //   console.log(vars)
    //   vars.forEach(item => {
    //     let pair = item.split('=')
    //     if(pair[0] == 'flag'){
    //       // console.log(pair[1])
    //       this.flag = pair[1]
    //       // console.log(this.flag)
    //     }
    //   })
    //   if(!this.flag == 'true') return;
    //   console.log('111')
    //   vars.forEach(item => {
    //     let pair = item.split('=')
    //     if(pair[0] == 'patientId'){
    //       this.patientId = pair[1]
    //       // console.log(this.patientId)
    //     }
    //   })

    //   this.$router.push({
    //     name: "inpatients",
    //     query: {patientId: this.patientId, flag: this.flag}
    //   });
    //   console.log('222')
    // },
    autoLogin() {
      // console.log(window.location)

      let query = window.location.search.substring(1);
      let vars = query.split("&");
      console.log(vars);

      vars.forEach(item => {
        let pair = item.split("=");
        if (pair[0] == "flag") {
          // console.log(pair[1])
          this.flag = pair[1];
          // console.log(this.flag)
        }
      });

      // console.log(typeof(this.flag))
      if (this.flag != "true") return;

      vars.forEach(item => {
        let pair = item.split("=");
        if (pair[0] == "patientId") {
          this.patientId = pair[1];
          console.log(this.patientId);
        }
      });

      this.userName = "林杰";
      this.password = "123";

      this.doLogin();
    },
    //点击登录调用方法
    doLogin() {
      // alert(1111)
      //保存的账号
      let name = this.userName;
      //保存的密码
      let pass = this.password;
      // console.log(name);

      if (name == "" || name == null) {
        this.$toast.center("用户名不能为空");
        return false;
      } else if (pass == "" || pass == null) {
        this.$toast.center("密码不能为空");
        return false;
      } else {
        // 调用api接口，并且提供了两个参数

        apiLogin({
          loginName: name,
          password: pass
        })
          .then(res => {
            // 获取数据成功后的其他操作
            //console.log(res);
            if (res.Status == 1) {
              // 判断复选框是否被勾选 勾选则调用配置cookie方法
              if (this.checked == true) {
                // console.log("checked == true");
                //传入账号名，密码，
                localStorage.setItem("account", JSON.stringify(name));
                localStorage.setItem("password", JSON.stringify(pass));
              } else {
                //清空登陆人信息
                localStorage.removeItem("account");
                localStorage.removeItem("password");
                // console.log("sessionStorage");
              }
              sessionStorage.setItem("token", true);
              // _this.$store.dispatch("login");
              let loginUser = new Object();
              // console.log(JSON.parse(res.Data)[0].Name);
              loginUser.Name = JSON.parse(res.Data)[0].Name;
              loginUser.Position = JSON.parse(res.Data)[0].Position;
              loginUser.Content = JSON.parse(res.Data)[0].Content;
              loginUser.ID = JSON.parse(res.Data)[0].ID;
              // alert(JSON.stringify(loginUser))
              sessionStorage.setItem("user", JSON.stringify(loginUser));
              sessionStorage.setItem("loginType", JSON.parse(res.Data)[0].type);
              //跳转到首页
              // alert('OK')
              this.$router.push({
                name: "inpatients",
                query: { item: 0, department: 1, patientId: this.patientId }
                // params: { userName: JSON.parse(res.Data)[0].Name, Position:JSON.parse(res.Data)[0].Position,Contents:JSON.parse(res.Data)[0].Content}
              });
              // _this.$router.push({path:'/inpatients',query:{orderId: 123}});
            } else {
              this.$toast.center(res.Message);
              // alert(response.data.Message);
            }
          })
          .catch(err => {
            //this.$toast.center('网络错误，请联系管理员');
            this.$toast.center(err);
            //console.log(err)
          });
      }
    }
  }
};
</script>
<style scoped>
.login_head {
  background-color: #fff;
}

.head-content {
  padding-top: 3.5%;
  padding-bottom: 30px;
}

.head-content .logo {
  height: 90px;
  padding-left: 140px;
}
.head-content .logo img {
  height: 100%;
}

.login_main {
  position: relative;
  background: url("../../../static/images/login_bg.jpg") no-repeat center center;
  background-size: cover;
  padding: 0;
}

.ctn_box {
  height: 500px;
  margin-top: 30px;
  margin-bottom: 45px;
}
.content_title {
  position: absolute;
  top: 0;
  left: 0;
  height: 572px;
}
.content_title > div {
  position: absolute;
  width: 100%;
  top: 35%;
  right: 50px;
}
.content_title h1 {
  color: #fff;
  font-size: 40px;
  font-weight: bold;
}
.content_title h3 {
  color: #fff;
  font-size: 28px;
}
.form-menu {
  width: 450px;
  padding: 0 35px;
  background-color: #fff;
  box-sizing: border-box;
  border: 30px solid rgba(255, 255, 255, 0.5);
  background: white;
  background-clip: padding-box;
}
.input-title {
  font-size: 26px;
  color: #333;
  margin-top: 78px;
  margin-bottom: 15px;
}
.c666 {
  color: #666;
}
.loginLine {
  position: relative;
  width: 100%;
  height: 1px;
  background: #999;
  margin-bottom: 35px;
}
.loginLine:before {
  content: "";
  position: absolute;
  width: 40%;
  height: 1px;
  background-color: #11a7f3;
  left: 50%;
  margin-left: -20%;
}
.login-inputBox {
  border: 1px solid #e3e3e3;
  border-radius: 4px;
  margin-bottom: 20px;
  height: 54px;
  background-color: #fff;
}

.login-inputBox input {
  border: none;
  width: 100%;
  height: 52px;
  margin: 0;
  padding-left: 8px;
  box-sizing: border-box;
  font-size: 16px;
  color: #666666;
}

.login-inputBox > div {
  width: 84%;
  height: 100%;
}

.login-inputBox label {
  height: 32px;
  width: 16%;
  border-right: 1px solid #e3e3e3;
  margin-top: 10px;
  margin-bottom: 10px;
  box-sizing: border-box;
  text-align: center;
  line-height: 32px;
}

.loginLable1 i {
  display: inline-block;
  width: 25px;
  height: 25px;
  background-image: url("../../../static/images/login_user.png");
  background-repeat: no-repeat;
}

.loginLable2 i {
  display: inline-block;
  width: 22px;
  height: 29px;
  background-image: url("../../../static/images/login_pwd.png");
  background-repeat: no-repeat;
}
.submit-btn {
  height: 54px;
  line-height: 54px;
  text-align: center;
  color: #fff;
  background-color: #11a8f2;
  border-radius: 3px;
  width: 100%;
  border: none;
  font-size: 24px;
  margin-bottom: 65px;
}

.version {
  text-align: center;
  line-height: 70px;
  height: 70px;
  font-size: 16px;
  color: #666;
  margin: 0;
}
.wrapper {
  margin-bottom: 20px;
}
.wrapper span {
  position: relative;
}
.wrapper .input_check {
  position: absolute;
  visibility: hidden;
}
.wrapper label {
  margin-bottom: 0;
  font-size: 16px;
  font-weight: 500;
  vertical-align: bottom;
}
.wrapper .input_check + label {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 1px solid #999;
  vertical-align: top;
  cursor: pointer;
  margin-right: 5px;
}
.wrapper .input_check:checked + label {
  border: 1px solid #11a8f2;
}
.wrapper .input_check:checked + label:after {
  content: "";
  position: absolute;
  left: 2px;
  bottom: 13px;
  width: 27px;
  height: 10px;
  border: 2px solid #11a8f2;
  border-top-color: transparent;
  border-right-color: transparent;
  -ms-transform: rotate(-60deg);
  -moz-transform: rotate(-60deg);
  -webkit-transform: rotate(-60deg);
  transform: rotate(-45deg);
}

@media (min-width: 1360px) {
  .container {
    width: 1280px;
  }
}

@media screen and (max-width: 1400px) {
  /*(min-width:1280) and*/
  .ctn_box {
    height: 380px;
    margin-top: 30px;
    margin-bottom: 30px;
  }

  .input-title {
    font-size: 22px;
    margin-top: 45px;
    margin-bottom: 20px;
  }

  .login-inputBox label {
    height: 36px;
    margin-top: 4px;
    margin-bottom: 4px;
  }
  .login-inputBox {
    border: 1px solid #e3e3e3;
    border-radius: 4px;
    margin-bottom: 20px;
    height: 40px;
  }

  .login-inputBox input {
    height: 38px;
  }

  .submit-btn {
    height: 40px;
    line-height: 40px;
    font-size: 18px;
    margin-bottom: 50px;
  }
  .content_title {
    position: absolute;
    top: 0;
    left: 0;
    height: 452px;
  }
  .content_title > div {
    position: absolute;
    width: 100%;
    top: 35%;
    right: 25px;
  }
  .content_title h1 {
    font-size: 34px;
    font-weight: bold;
  }
  .content_title h3 {
    font-size: 24px;
  }
}

@media screen and (max-width: 1200px) {
  .head-content {
    padding-bottom: 20px;
  }
  .form-menu {
    width: 320px;
    padding: 0 35px;
    background-color: #fff;
    box-sizing: border-box;
    border: 18px solid rgba(255, 255, 255, 0.5);
  }
  .head-content .logo {
    height: 70px;
    padding-left: 100px;
  }
  .ctn_box {
    height: 365px;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .loginLine {
    position: relative;
    width: 100%;
    height: 1px;
    background: #999;
    margin-bottom: 25px;
  }
  .loginLable1 i {
    width: 18px;
    height: 18px;
    background-size: cover;
  }
  .loginLable2 i {
    width: 16px;
    height: 20px;
    background-size: cover;
  }
  .content_title {
    position: absolute;
    top: 0;
    left: 0;
    height: 378px;
  }
  .content_title > div {
    position: absolute;
    width: 100%;
    top: 35%;
    right: 20px;
  }
  .content_title h1 {
    font-size: 28px;
    font-weight: bold;
  }
  .content_title h3 {
    font-size: 20px;
  }
  .input-title {
    font-size: 18px;
    margin-top: 40px;
    margin-bottom: 12px;
  }

  .login-inputBox label {
    height: 24px;
    margin-top: 4px;
    margin-bottom: 4px;
  }

  .login-inputBox {
    border: 1px solid #e3e3e3;
    border-radius: 4px;
    margin-bottom: 20px;
    height: 38px;
  }

  .login-inputBox input {
    height: 36px;
  }

  .submit-btn {
    height: 36px;
    line-height: 36px;
    font-size: 16px;
    margin-bottom: 40px;
  }
  .wrapper .input_check + label {
    vertical-align: middle;
    width: 16px;
    height: 16px;
    margin-right: 5px;
  }
  .wrapper .input_check:checked + label {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 1px solid #11a8f2;
    vertical-align: middle;
    cursor: pointer;
    margin-right: 5px;
  }
  .wrapper .input_check:checked + label:after {
    content: "";
    position: absolute;
    left: 2px;
    bottom: 10px;
    width: 21px;
    height: 10px;
    border: 2px solid #11a8f2;
    border-top-color: transparent;
    border-right-color: transparent;
    -ms-transform: rotate(-60deg);
    -moz-transform: rotate(-60deg);
    -webkit-transform: rotate(-60deg);
    transform: rotate(-45deg);
  }
  .wrapper label {
    margin-bottom: 0;
    font-size: 12px;
    font-weight: 500;
  }
  .version {
    text-align: center;
    line-height: 60px;
    height: 60px;
    font-size: 14px;
    color: #666;
    margin: 0;
  }
}
@media (max-width: 768px) {
  /* 768修改登录页底部文字 */
  .version {
    text-align: center;
    line-height: 40px;
    height: 40px;
    font-size: 12px;
    color: #666;
    margin: 0;
  }
  .head-content .logo{
    height: 40px;
    padding-left: 50px;
  }  
  .form-menu {
    width: 320px;
    padding: 0 35px;
    background-color: #fff;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    border: 18px solid rgba(255, 255, 255, 0.5);
  } 
}
@media screen and (max-width: 425px){
  .ctn_box{
    float: initial!important;
    height: 395px;
    padding-top: 15px;
    margin-top: 0px;
    margin-bottom: 0px;
  }
  .form-menu{
    margin: auto;
    width: 290px;
  }
}
</style>
